package cn.ichenkun.web.servlet;

import cn.ichenkun.util.JDBCUtils;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.support.json.JSONWriter;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/listServlet")
public class ListServlet extends HttpServlet {


    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");

        String sql = "SELECT * FROM `group`";
        String userSql = "SELECT * FROM `user`";
        //2.执行sql
        List group = template.queryForList(sql);
        List user = template.queryForList(userSql);
        List ret = new ArrayList();
        ret.add(group);
        ret.add(user);
        String json = JSONUtils.toJSONString(ret);

        PrintWriter out = response.getWriter();
        out.println(json);
        out.flush();
        out.close();

    }
}
